﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BGSpaceShooter;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ParticleSystem.ParticleSystem;

namespace ParticleSystem
{
    public class WaterEffect : IObjectEffect
    {
        private bool isActive;
        public GameObject GameObject { get; set; }

        public ParticleEmitter particleEmmiter;


        public WaterEffect(GameObject gameObject)
        {
            GameObject = gameObject;

            CreateParticleEmitter();
        }

        private void CreateParticleEmitter()
        {
            particleEmmiter = new ParticleEmitter();

            IsActive = false;

            particleEmmiter.MaxParticles = 100;

            particleEmmiter.Duration = TimeSpan.FromSeconds(0.00001f);
            particleEmmiter.DurationRandomness = 0;
            particleEmmiter.MinHorizontalVelocity = 0;
            particleEmmiter.MaxHorizontalVelocity = 0;

            particleEmmiter.MinVerticalVelocity = 0;
            particleEmmiter.MaxVerticalVelocity = 0;

            particleEmmiter.Gravity = new Vector3(0, 0, 0);

            particleEmmiter.EndVelocity = 0;

            particleEmmiter.MinRotateSpeed = -10;
            particleEmmiter.MaxRotateSpeed = 10;

            particleEmmiter.MinStartSize = 0.8f;
            particleEmmiter.MaxStartSize = 0.8f;

            particleEmmiter.MinColor = new Color(200, 200, 150, 0.1f);
            particleEmmiter.MaxColor = new Color(255, 255, 200, 0.9f);
            particleEmmiter.BlendState = BlendState.AlphaBlend;

            particleEmmiter.MinEndSize = 0.8f;
            particleEmmiter.MaxEndSize = 0.8f;

            particleEmmiter.Reset();

            particleEmmiter.LoadContent(Display.GraphicsDevice);
            particleEmmiter.LoadParticleEffect(GameContentManager.Content.GetTexture("water"));
        }

        #region IObjectEffect Members

        public bool IsActive
        {
            get { return isActive; }
            set
            {
                isActive = value;
                //  particleEmmiter.Reset();
            }
        }

        public bool ShouldVibrate()
        {
            return false;
        }

        public EffectDrawer GetDrawer()
        {
            if (GameObject == null)
            {
                throw new NullReferenceException("No GameObject assigned");
            }
            return new WaterEffectDrawer(this, GameObject);
        }

        public void Update(GameTime gameTime)
        {
            if (GameObject == null)
            {
                throw new NullReferenceException("No GameObject assigned");
            }
            Vector3 enginesPosition =
                        ((GameObject.Model.Meshes["Engines"].ParentBone.Transform) * GameObject.ModelMatrix).Translation;
            for (int i = 0; i < 3; i++)
                if (this.IsActive)
                {
                    Vector3 speed = Vector3.Transform(new Vector3(0, 0, -200f), GameObject.RotationMatrix);
                    var engineOffset = Vector3.Transform(new Vector3(0, -0.28f, -4), GameObject.RotationMatrix);
                    particleEmmiter.AddParticle(enginesPosition - engineOffset, speed);

                    engineOffset = Vector3.Transform(new Vector3(-0.7f, 0.28f, -4), GameObject.RotationMatrix);
                    particleEmmiter.AddParticle(enginesPosition - engineOffset, speed);

                    engineOffset = Vector3.Transform(new Vector3(0.7f, 0.28f, -4), GameObject.RotationMatrix);
                    particleEmmiter.AddParticle(enginesPosition - engineOffset, speed);

                }
            particleEmmiter.Update(gameTime);
        }

        #endregion
    }
}
